package com.sservice.web.utils;

import org.apache.log4j.Logger;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import java.util.Properties;

public class MailSender {

    private Logger logger = Logger.getLogger(MailSender.class);

    private String smtp_addr = null;
    private Boolean smtp_auth = false;
    private String sender = null;
    private String password = null;

    public MailSender(final String smtpAddr, boolean smtpAuth,
                      final String sender, final String password) {
        this.smtp_addr = smtpAddr;
        this.smtp_auth = smtpAuth;
        this.sender = sender;
        this.password = password;
    }

    private boolean doSend(final String sender, final String password,
                           final MailReceiver receiver, final String subject, String content,
                           final String files[]) {
        Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", smtp_auth.toString());
        props.put("mail.smtp.host", smtp_addr);
        props.put("mail.smtp.port", "25");

        javax.mail.Session session = Session.getDefaultInstance(props);
        session.setDebug(true);

        MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(sender));
            message.addRecipients(Message.RecipientType.TO, receiver.getTO());
            if (receiver.getCC() != null) {
                message.addRecipients(Message.RecipientType.CC, receiver
                        .getCC());
            }
            if (receiver.getBCC() != null) {
                message.addRecipients(Message.RecipientType.BCC, receiver
                        .getBCC());
            }
            message.setSubject(subject, "UTF-8");
            message.setSentDate(new java.util.Date());
            message.setText(content, "UTF-8");
            if (files.length > 0) {
                Multipart multipart = new MimeMultipart();
                for (int i = 0; i < files.length; i++) {
                    MimeBodyPart messageBodyPart = new MimeBodyPart();
                    String fileName = files[i];
                    String displayName = fileName.substring(fileName
                            .lastIndexOf("/") + 1);
                    FileDataSource fds = new FileDataSource(fileName);
                    messageBodyPart.setDataHandler(new DataHandler(fds));
                    messageBodyPart.setFileName(MimeUtility
                            .encodeText(displayName));
                    multipart.addBodyPart(messageBodyPart);
                }
                message.setContent(multipart);
            }
            message.saveChanges();

            Transport transport = session.getTransport("smtp");
            transport.connect(smtp_addr, sender, password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return false;
        }

        return true;
    }

    public boolean send(final String receiver, final String subject,
                        final String content) {
        if (receiver == null || receiver.trim().length() == 0) {
            return false;
        }

        return doSend(sender, password, new MailReceiver(receiver, null, null),
                subject, content, new String[]{});
    }

    public boolean send(final String receiver, final String subject,
                        final String content, final String files[]) {
        if (receiver == null || receiver.trim().length() == 0) {
            return false;
        }

        return doSend(sender, password, new MailReceiver(receiver, null, null),
                subject, content, files);
    }

    public boolean send(final MailReceiver receiver, final String subject,
                        final String content) {
        return doSend(sender, password, receiver, subject, content, new String[]{});
    }

    public boolean send(final MailReceiver receiver, final String subject,
                        final String content, final String files[]) {
        return this.doSend(sender, password, receiver, subject, content, files);
    }

    public class MailReceiver {
        private String TO = null;
        private String CC = null;
        private String BCC = null;

        public MailReceiver(final String TO, final String CC, final String BCC) {
            this.TO = TO;
            this.CC = CC;
            this.BCC = BCC;
        }

        public String getTO() {
            return TO;
        }

        public void setTO(String to) {
            TO = to;
        }

        public String getCC() {
            return CC;
        }

        public void setCC(String cc) {
            CC = cc;
        }

        public String getBCC() {
            return BCC;
        }

        public void setBCC(String bcc) {
            BCC = bcc;
        }
    }
}
